from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# 加载sklearn自带的数据
sample_data = datasets.load_digits()
images = sample_data.data
labels = sample_data.target

# 划分训练集和测试机
train_date, test_data, train_labels, test_labels = train_test_split(images, labels, test_size=0.1)
# 选择模型
model_knn = KNeighborsClassifier(n_neighbors=4, algorithm='auto', weights='distance')
# 训练模型
model_knn.fit(train_date, train_labels)
# 预测
pre = model_knn.predict(test_data)
# 查看准确率
acc = accuracy_score(pre, test_data)
